<?
// init site 初始化网站
include("configure.php");

// tools 函数库
include(ROOT."common/common.php");
include(ROOT."common/cookie.php");
include(ROOT."common/class_html.php"); // 网页组件
require ROOT.'common/encrypt.php'; // 加密解密 encrypt

// models 模型
include(ROOT."model/table.php");
include(ROOT."model/class_db.php");
include(ROOT."model/class_customer.php");
include(ROOT."model/class_user.php");
include(ROOT."model/class_product.php");
include(ROOT."model/class_product_review.php");

// 实例化工具类 tool objects
$h = $html = new Html();

// 实例化数据类 objects
// 根据需要选择开/关 enable them when needed
$conn=null;
$db=new DB();
$conn=$db->get_conn();

$configuration=new DB(Tconfiguration,$conn);
//$address_book=new DB(Taddress_book,$conn);
$ad=new DB(Tad,$conn);
$ad_category=new DB(Tad_category,$conn);
$about=new DB(Tabout,$conn);
//$menu=new DB(Tmenu,$conn);
$document=new DB(Tdocument,$conn);
$document_category=new DB(Tdocument_category,$conn);
//$document_pic=new DB(Tdocument_pic,$conn);
//$express_company=new DB(Texpress_company,$conn);

$article=new DB(Tarticle,$conn);
$article_category=new DB(Tarticle_category,$conn);
$banner=new DB(Tbanner,$conn);
$city=new DB(Tcity,$conn);
$code_lookup=new DB(Tcode_lookup,$conn);
$country=new DB(Tcountry,$conn);
$county=new DB(Tcounty,$conn);
$coupon=new DB(Tcoupon,$conn);
$cross_sell=new DB(Tcross_sell,$conn);
$currency=new DB(Tcurrency,$conn);
$customer=new Customer($conn);
//$department=new DB(Tdepartment,$conn);
$feedback=new DB(Tfeedback,$conn);
$grade=new DB(Tgrade,$conn);
$language=new DB(Tlanguage,$conn);
$module=new DB(Tmodule,$conn);
$news=new DB(Tnews,$conn);
$news_category=new DB(Tnews_category,$conn);
$optional_field=new DB(Toptional_field,$conn);
$payment_method=new DB(Tpayment_method,$conn);
$point=new DB(Tpoint,$conn);
$product=new Product($conn);
$product_category=new DB(Tproduct_category,$conn);
$product_pic=new DB(Tproduct_pic,$conn);
//$product_qa=new DB(Tproduct_qa,$conn);
$product_review=new Product_review($conn);
$product_variation=new DB(Tproduct_variation,$conn);
$product_option_value=new DB(Tproduct_option_value,$conn);
$product_option=new DB(Tproduct_option,$conn);
$province=new DB(Tprovince,$conn);
$refund=new DB(Trefund,$conn);
$refund_detail=new DB(Trefund_detail,$conn);
$refund_reason=new DB(Trefund_reason,$conn);
//$role=new DB(Trole,$conn);
//$role_module=new DB(Trole_module,$conn);
$sales_order=new DB(Tsales_order,$conn);
//$sales_order_detail=new DB(Tsales_order_detail,$conn);
$shipment_cost=new DB(Tshipment_cost,$conn);
//$shipment_method=new DB(Tshipment_method,$conn);
$user = new User($conn);
//$user_department=new DB(Tuser_department,$conn);
//$user_role=new DB(Tuser_role,$conn);

//$version=new DB(Tversion,$conn);
$wishlist=new DB(Twishlist,$conn);
$vice_menu=new DB(Tvice_menu, $conn);
$vice_menu_category=new DB(Tvice_menu_category, $conn);
$keyword=new DB(Tkeyword, $conn);

/************seven start**************/
$customer_rule = $grade;// 会员级别
$sales_promotion = $sales_promotion_list=  new DB(Tsales_promotion, $conn); // 促销
$promotion_fullcut = new DB(Tpromotion_fullcut, $conn); // 满减
$order_service = new DB(Torder_service, $conn);
$tags = new DB(Ttags, $conn);
$tag_list = new DB(Ttag_list, $conn);
$template = new DB(Ttemplate, $conn);
$recode = new DB(Trecode, $conn);
/************seven end****************/

// option array 选项开关
$option=array();
$result=$optional_field->get_rows(1024);
foreach($result as $row){
	$option[$row['table_name']][$row['field_name']]=$row['is_enabled'];
}
//pre($option);

// module array 模块开关
$option_module=array();
$result=$module->get_rows(1024);
foreach($result as $row){
	$option_module[$row['name']]=$row['is_enabled'];
}
//pre($option_module);

// 初始化配置 init configure
$config=array();
$result=null;
$result=$configuration->get_rows(1024);
foreach($result as $row){
	define($row['name'],$row['value']);
	$config[$row['name']]=$row['value'];
}

// 初始化语言, init language
$result=null;
$arrlang=array();
$result=$language->get_rows();
foreach($result as $row){
	if($row['is_enabled']) { $arrlang[$row['id']]=$row;} // arrlang = enabled langues, arrlang只表示启用的语言
	if($row['is_default']){ $lang=$row['code'];  }
	$option[$row['code']]=$row['is_enabled'];
	$option[strtoupper($row['code'])]=$row['is_enabled'];
	$arrlangcode[$row['code']]=$row;  // arrlangcode = all langues, arrlangcode表示所有的语言
	$arrlangnum[$row['id']]=$row;  // arrlangcode = all langues, arrlangcode表示所有的语言
}
// selected language 选定语言
// backend shall not care about language in cookie
if(strpos($_SERVER['REQUEST_URI'], '/cart/root/') === false && isset($_COOKIE['lang'])) $lang=$_COOKIE['lang'];
$lang_name=$arrlangcode[$lang]['name'];
$lang_id=$arrlangcode[$lang]['id'];
define('LANG',strtoupper($lang));

// 初始化货币, init currency
$result=null;
$arrcurrency=array();
$result=$currency->get_rows();
foreach($result as $row){
	if($row['is_enabled']) { $arrcurrency[$row['id']]=$row;} 
	if($row['is_default']){
		$currency_id=$row['id']; //subject to be changed by cookie
		$db_default_currency_id=$currency_id;
	}
}
// selected currenty 选定货币
if(isset($_COOKIE['currency'])) $currency_id=$_COOKIE['currency'];
$currency_code=$arrcurrency[$currency_id]['code'];
$currency_sign=$arrcurrency[$currency_id]['sign'];
$currency_rate=$arrcurrency[$currency_id]['default_to_rate'];

// code_lookup 预设枚举变量
// e.g. array('order_status'=array(1=>array('name_cn'=>'代付款')));
$code_array=array();
$result=$code_lookup->get_rows(4096,'','','seq_no','asc');
foreach($result as $row){
	$code_array[$row['name']][$row['value']]['name_'.$arrlangnum[$row['language_id']]['code']]=$row['description'];
	$code_array[$row['name']][$row['value']]['id']=$row['value'];
}
//pre($code_array);

// 是否为首页, 用于显示友情连接
$index=0;

// 图片是否为外部源 external image
if(!empty($config['PRODUCT_PIC_EXTERNAL_LINK'])) {
	define('PRODUCT_PIC_HOST',$config['PRODUCT_PIC_EXTERNAL_LINK']);
}
else {
	define('PRODUCT_PIC_HOST',$baseurl.'upload/');
}

// 默认SEO标签, default SEO tags
$title=$config['SITE_TITLE_'.LANG]; $kwd=$config['SITE_KEYWORD_'.LANG]; $dpn=$config['SITE_DESCRIPTION_'.LANG];

// end of file init